package com.example.listenerrecorder;

import android.content.Context;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;
import android.widget.Toast;

import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraView;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class PhoneListener extends PhoneStateListener {

    private TelephonyManager telephonyManager;//电话管理器
    private Context context;

    private OnPhoneListener onPhoneListener;//电话监听器
    private boolean BeCalled = false;//电话状态
    private CameraView cameraView ;//用于来电时拍照的第三方类库



    public PhoneListener(Context context) {
        this.context = context;
//        start();
    }
    public PhoneListener(Context context,CameraView camera) {
        this.context = context;
        cameraView=camera;
//        start();
    }

    public interface OnPhoneListener {
        void onCall();
    }

    public void setOnPhoneListener(OnPhoneListener listener) {
        onPhoneListener = listener;
    }


    public void start(TextView textView) {

        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if (telephonyManager != null) {
            telephonyManager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
            textView.setText("电话监听器:正在运行");
            System.out.println("电话监听器创建成功");
        } else {
            System.out.println("电话监听器创建失败");
        }
    }

    public void stop(TextView textView) {
        System.out.println("电话监听结束");
        telephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
        textView.setText("电话监听器:停止运行");
    }

    @Override
    //状态变化时的操作
    public void onCallStateChanged(int state, String number) {

        switch (state) {

            //case TelephonyManager.C


            case TelephonyManager.CALL_STATE_RINGING: //电话铃响时
            {
                //相机的操作
                cameraView.addCameraListener(new CameraListener() {
                    @Override
                    //重写拍照方法
                    public void onPictureTaken(byte[] picture) {

                        Date time;
                        time=new Date();
                        String str_time = time.toLocaleString();//获取当前时间
                        //保存图片在根目录
                        File savedPhoto = new File(Environment.getExternalStorageDirectory(), "监听拍照"+str_time+".jpg");
                        // File savedPhoto=new File(Environment.getExternalStoragePublicDirectory(), "/拍照:" + str_time + ".jpg");
                        try {
                            //图片文件输出
                            FileOutputStream outputStream = new FileOutputStream(savedPhoto.getPath());
                            outputStream.write(picture);
                            outputStream.close();
                        } catch (java.io.IOException e) {
                            e.printStackTrace();
                        }

                    }
                });

//间隔三秒钟
                TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        //执行拍照
                        cameraView.capturePicture();
                        Toast.makeText(context,"已拍照",Toast.LENGTH_LONG).show();
                        /**
                         *要执行的操作
                         */
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task, 3000);//3秒后执行TimeTask的run方法

//设置电话监听器oncall方法，方法已经在mainactivity内重写，使得根据电话状态执行操作
                System.out.println("检测到通话");
                onPhoneListener.onCall();
                BeCalled = true;
            }
                break;


            case TelephonyManager.CALL_STATE_IDLE://挂断电话时
            {

                if (BeCalled) {
                    System.out.println("检测到挂断");
                    onPhoneListener.onCall();
                }

            }
            break;

            default:
                break;

        }
        super.onCallStateChanged(state, number);
    }
}
